package cn.joeg.app.web.db.user;

import cn.joeg.app.css.util.DBHelper;
import cn.joeg.app.css.util.date.TimeUtil;
import cn.joeg.app.web.db.util.DBInput;
import cn.joeg.app.web.db.util.DBInvoker;
import cn.joeg.app.web.db.util.DBOutput;
import cn.joeg.app.web.db.util.DBUtil;
import cn.joeg.app.web.util.Const;
import com.ecfront.dew.common.JsonHelper;
import com.ecfront.dew.common.Resp;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;


@Service
public class UserService extends DBHelper implements DBInvoker<UserService.Input, UserService.Output> {

    private static final Logger logger = LoggerFactory.getLogger(UserService.class);


    @Autowired
    private DBUtil dbUtil;


    @Override
    public Resp<Output> execute(Input input) {

        return null;
    }


    public static class Input extends DBInput {
    }

    public static class Output extends DBOutput {
    }

    public int createUser(String deviceId, String mac, String idfa) {
        return dbUtil.psUpdate("insert into starwork_user (deviceid,createtime,mac,idfa) values (?,?,?,?)",
                new Object[]{
                        deviceId, TimeUtil.getString(TimeUtil.sf), mac == null ? "" : mac,
                        idfa == null ? "" : idfa
                });
    }


    public void _360Login(String id360, String deviceId) {

        try {
            long size = dbUtil.psQueryCounts("select count(*) from starwork_user where deviceid='" + deviceId + "'");
            if (size > 0) {
                dbUtil.executeUpdate("update starwork_user set deviceid='" + id360 + "' where deviceid='" + deviceId + "'");
            }
        } catch (Exception e) {
            logger.error("UserService:_360Login:error{}", e.getMessage());
        }
    }

    public List<UserVO> queryUserByDeviceId(String value, String deviceId) {
        value = value == null || value.isEmpty() ? "*" : value;
        List<Map<String, Object>> resList = dbUtil.psQuery("select " + value + " from starwork_user where deviceid=?", new Object[]{deviceId});
        return JsonHelper.toList(resList, UserVO.class);
    }

    public UserVO queryUserById(String value, String id) {
        value = value == null || value.isEmpty() ? "*" : value;
        List<Map<String, Object>> resList = dbUtil.psQuery("select " + value + " from starwork_user where id=?", new Object[]{id});
        if (!resList.isEmpty()) {
            return JsonHelper.toObject(resList.get(0), UserVO.class);
        }
        return null;
    }

    public boolean updatePhoneNum(int id, String phoneNum, String password) {
        String sql = "update starwork_user set phonenum=? ,password=? where id=?";
        return dbUtil.executeUpdate(sql, phoneNum, password, id) > 0;
    }

    public List<UserVO> queryUserByPhoneNum(String phoneNum) {
        List<Map<String, Object>> resList = dbUtil.psQuery("select * from starwork_user where phonenum=?", new Object[]{phoneNum});
        return JsonHelper.toList(resList, UserVO.class);
    }

    public static class UserVO {

        private int id;                           //  用户id,
        private int refreshmoneystore;            //  包月玩家免费雇佣的截止unix时间戳,
        private int refreshdiamondstore;          //  非包月玩家每日剩余的雇佣次数,
        private int lottery;                      //  抽卡次数,
        private String deviceid;                  //  设备id,
        private String mac;                       //  mac地址,
        private String idfa;                      //  idfa标记,
        private String password;                  //  登录密码,
        private String phonenum;                  //  手机号,
        private String createtime;                //  首次登录时间,
        private String lasttime;                  //  最后登录时间,
        private int sex;                          //  性别（0：男 1：女 2：保密）,
        private int blocked;                      //  是否被封号（0：没有封号 1：被封号）,
        private int type;                         //  用户类型（1：系统管理员 2：GM 3：玩家）,
        private String name;                      //  ,
        private String companyname;               //  ,
        private int companylogo;                  //  公司logo,
        private int companylevel;                 //  公司等级,
        private int companyexp;                   //  公司经验,
        private int artistnumber;                 //  艺人上限,
        private int money;                        //  公司资产,
        private int diamond;                      //  收费币,
        private int hasdiamond;                   //  充值的钱,
        private int paystatus;                    //  ????,
        private int uservip;                      //  VIP等级,
        private String cardidarray;               //  装备的卡牌id,
        private String cardindexarray;            //  战队卡牌index,
        private int missionid;                    //  当前任务的id,
        private int missionstatus;                //  当前任务的状态,
        private String cardidbattle;              //  战斗中获得的卡牌,
        private String cardidnobattle;            //  并战斗中获得的卡牌,
        private int wincount;                     //  胜利次数,
        private int losecount;                    //  失败次数,
        private String arenainfo;                 //  竞技场信息,
        private int fansid;                       //  0,
        private int fansdonate;                   //  0,
        private int fansremain;                   //  0,
        private int fanstoday;                    //  0,
        private int fansbuffer;                   //  0,
        private int fansbuffertime;               //  0,
        private int homenotice;                   //  是否有内部通告,
        private int spreadserver;                 //  推广者服务器,
        private int spreadid;                     //  0,
        private int hascode;                      //  是否兑换过code,
        private int artdigest;                    //  艺能精华数量,
        private int partdigest;                   //  强效艺能精华数量,
        private String dailyvalue;                //  每日任务完成度,
        private String dailystatus;               //  0-未完成，1-已完成,
        private int girlcompanyexp;               //  女生版公司经验,
        private int girlcompanylevel;             //  女生版公司等级,
        private String vipdrawstatus;             //   //  vip领取状态，0-不可领，1-可伶，2-已领,
        private int datenum;                        //  0,
        private int cardtotalpower;                //  卡牌总实力,
        private String cardtoppowericons;        //  卡牌总实力,
        private String cardtoppowerstarlevels;    //  实力榜卡牌星级,
        private String cardtoppowerprolevels;    //  实力榜卡牌职业经验等级,
        private String cardtoppowerprotypes;        //  实力榜卡牌职业经验类型,
        private String cardtoppowerpops;            //  实力榜卡牌人气,
        private String cardtoppowersexs;            //  实力榜卡牌人气,
        private String noticeidxs;                //  通告id集合,
        private String noticecids;                //  通告对应的卡牌id集合,
        private String noticestatus;                //  通告状态集合,
        private String noticeleftimes;            //  通告剩余时间集合,单位秒,
        private String noticequalitys;            //  通告品质集合,
        private int notiecuptime;                //  通告变更时间,
        private int powersearchlevel;            //  精英探索关卡,
        private int powersearchpercent;            //  精英探索关卡完成度,
        private int houruptime;                    //  通告变更时间,
        private String giftpack;
        private String userboss;
        private int zodiac_score;                 //  0,

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getRefreshmoneystore() {
            return refreshmoneystore;
        }

        public void setRefreshmoneystore(int refreshmoneystore) {
            this.refreshmoneystore = refreshmoneystore;
        }

        public int getRefreshdiamondstore() {
            return refreshdiamondstore;
        }

        public void setRefreshdiamondstore(int refreshdiamondstore) {
            this.refreshdiamondstore = refreshdiamondstore;
        }

        public int getLottery() {
            return lottery;
        }

        public void setLottery(int lottery) {
            this.lottery = lottery;
        }

        public String getDeviceid() {
            return deviceid;
        }

        public void setDeviceid(String deviceid) {
            this.deviceid = deviceid;
        }

        public String getMac() {
            return mac;
        }

        public void setMac(String mac) {
            this.mac = mac;
        }

        public String getIdfa() {
            return idfa;
        }

        public void setIdfa(String idfa) {
            this.idfa = idfa;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getPhonenum() {
            return phonenum;
        }

        public void setPhonenum(String phonenum) {
            this.phonenum = phonenum;
        }

        public String getCreatetime() {
            return createtime;
        }

        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }

        public String getLasttime() {
            return lasttime;
        }

        public void setLasttime(String lasttime) {
            this.lasttime = lasttime;
        }

        public int getSex() {
            return sex;
        }

        public void setSex(int sex) {
            this.sex = sex;
        }

        public int getBlocked() {
            return blocked;
        }

        public void setBlocked(int blocked) {
            this.blocked = blocked;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCompanyname() {
            return companyname;
        }

        public void setCompanyname(String companyname) {
            this.companyname = companyname;
        }

        public int getCompanylogo() {
            return companylogo;
        }

        public void setCompanylogo(int companylogo) {
            this.companylogo = companylogo;
        }

        public int getCompanylevel() {
            return companylevel;
        }

        public void setCompanylevel(int companylevel) {
            this.companylevel = companylevel;
        }

        public int getCompanyexp() {
            return companyexp;
        }

        public void setCompanyexp(int companyexp) {
            this.companyexp = companyexp;
        }

        public int getArtistnumber() {
            return artistnumber;
        }

        public void setArtistnumber(int artistnumber) {
            this.artistnumber = artistnumber;
        }

        public int getMoney() {
            return money;
        }

        public void setMoney(int money) {
            this.money = money;
        }

        public int getDiamond() {
            return diamond;
        }

        public void setDiamond(int diamond) {
            this.diamond = diamond;
        }

        public int getHasdiamond() {
            return hasdiamond;
        }

        public void setHasdiamond(int hasdiamond) {
            this.hasdiamond = hasdiamond;
        }

        public int getPaystatus() {
            return paystatus;
        }

        public void setPaystatus(int paystatus) {
            this.paystatus = paystatus;
        }

        public int getUservip() {
            return uservip;
        }

        public void setUservip(int uservip) {
            this.uservip = uservip;
        }

        public String getCardidarray() {
            return cardidarray;
        }

        public void setCardidarray(String cardidarray) {
            this.cardidarray = cardidarray;
        }

        public String getCardindexarray() {
            return cardindexarray;
        }

        public void setCardindexarray(String cardindexarray) {
            this.cardindexarray = cardindexarray;
        }

        public int getMissionid() {
            return missionid;
        }

        public void setMissionid(int missionid) {
            this.missionid = missionid;
        }

        public int getMissionstatus() {
            return missionstatus;
        }

        public void setMissionstatus(int missionstatus) {
            this.missionstatus = missionstatus;
        }

        public String getCardidbattle() {
            return cardidbattle;
        }

        public void setCardidbattle(String cardidbattle) {
            this.cardidbattle = cardidbattle;
        }

        public String getCardidnobattle() {
            return cardidnobattle;
        }

        public void setCardidnobattle(String cardidnobattle) {
            this.cardidnobattle = cardidnobattle;
        }

        public int getWincount() {
            return wincount;
        }

        public void setWincount(int wincount) {
            this.wincount = wincount;
        }

        public int getLosecount() {
            return losecount;
        }

        public void setLosecount(int losecount) {
            this.losecount = losecount;
        }

        public String getArenainfo() {
            return arenainfo;
        }

        public void setArenainfo(String arenainfo) {
            this.arenainfo = arenainfo;
        }

        public int getFansid() {
            return fansid;
        }

        public void setFansid(int fansid) {
            this.fansid = fansid;
        }

        public int getFansdonate() {
            return fansdonate;
        }

        public void setFansdonate(int fansdonate) {
            this.fansdonate = fansdonate;
        }

        public int getFansremain() {
            return fansremain;
        }

        public void setFansremain(int fansremain) {
            this.fansremain = fansremain;
        }

        public int getFanstoday() {
            return fanstoday;
        }

        public void setFanstoday(int fanstoday) {
            this.fanstoday = fanstoday;
        }

        public int getFansbuffer() {
            return fansbuffer;
        }

        public void setFansbuffer(int fansbuffer) {
            this.fansbuffer = fansbuffer;
        }

        public int getFansbuffertime() {
            return fansbuffertime;
        }

        public void setFansbuffertime(int fansbuffertime) {
            this.fansbuffertime = fansbuffertime;
        }

        public int getHomenotice() {
            return homenotice;
        }

        public void setHomenotice(int homenotice) {
            this.homenotice = homenotice;
        }

        public int getSpreadserver() {
            return spreadserver;
        }

        public void setSpreadserver(int spreadserver) {
            this.spreadserver = spreadserver;
        }

        public int getSpreadid() {
            return spreadid;
        }

        public void setSpreadid(int spreadid) {
            this.spreadid = spreadid;
        }

        public int getHascode() {
            return hascode;
        }

        public void setHascode(int hascode) {
            this.hascode = hascode;
        }

        public int getArtdigest() {
            return artdigest;
        }

        public void setArtdigest(int artdigest) {
            this.artdigest = artdigest;
        }

        public int getPartdigest() {
            return partdigest;
        }

        public void setPartdigest(int partdigest) {
            this.partdigest = partdigest;
        }

        public String getDailyvalue() {
            return dailyvalue;
        }

        public void setDailyvalue(String dailyvalue) {
            this.dailyvalue = dailyvalue;
        }

        public String getDailystatus() {
            return dailystatus;
        }

        public void setDailystatus(String dailystatus) {
            this.dailystatus = dailystatus;
        }

        public int getGirlcompanyexp() {
            return girlcompanyexp;
        }

        public void setGirlcompanyexp(int girlcompanyexp) {
            this.girlcompanyexp = girlcompanyexp;
        }

        public int getGirlcompanylevel() {
            return girlcompanylevel;
        }

        public void setGirlcompanylevel(int girlcompanylevel) {
            this.girlcompanylevel = girlcompanylevel;
        }

        public String getVipdrawstatus() {
            return vipdrawstatus;
        }

        public void setVipdrawstatus(String vipdrawstatus) {
            this.vipdrawstatus = vipdrawstatus;
        }

        public int getDatenum() {
            return datenum;
        }

        public void setDatenum(int datenum) {
            this.datenum = datenum;
        }

        public int getCardtotalpower() {
            return cardtotalpower;
        }

        public void setCardtotalpower(int cardtotalpower) {
            this.cardtotalpower = cardtotalpower;
        }

        public String getCardtoppowericons() {
            return cardtoppowericons;
        }

        public void setCardtoppowericons(String cardtoppowericons) {
            this.cardtoppowericons = cardtoppowericons;
        }

        public String getCardtoppowerstarlevels() {
            return cardtoppowerstarlevels;
        }

        public void setCardtoppowerstarlevels(String cardtoppowerstarlevels) {
            this.cardtoppowerstarlevels = cardtoppowerstarlevels;
        }

        public String getCardtoppowerprolevels() {
            return cardtoppowerprolevels;
        }

        public void setCardtoppowerprolevels(String cardtoppowerprolevels) {
            this.cardtoppowerprolevels = cardtoppowerprolevels;
        }

        public String getCardtoppowerprotypes() {
            return cardtoppowerprotypes;
        }

        public void setCardtoppowerprotypes(String cardtoppowerprotypes) {
            this.cardtoppowerprotypes = cardtoppowerprotypes;
        }

        public String getCardtoppowerpops() {
            return cardtoppowerpops;
        }

        public void setCardtoppowerpops(String cardtoppowerpops) {
            this.cardtoppowerpops = cardtoppowerpops;
        }

        public String getCardtoppowersexs() {
            return cardtoppowersexs;
        }

        public void setCardtoppowersexs(String cardtoppowersexs) {
            this.cardtoppowersexs = cardtoppowersexs;
        }

        public String getNoticeidxs() {
            return noticeidxs;
        }

        public void setNoticeidxs(String noticeidxs) {
            this.noticeidxs = noticeidxs;
        }

        public String getNoticecids() {
            return noticecids;
        }

        public void setNoticecids(String noticecids) {
            this.noticecids = noticecids;
        }

        public String getNoticestatus() {
            return noticestatus;
        }

        public void setNoticestatus(String noticestatus) {
            this.noticestatus = noticestatus;
        }

        public String getNoticeleftimes() {
            return noticeleftimes;
        }

        public void setNoticeleftimes(String noticeleftimes) {
            this.noticeleftimes = noticeleftimes;
        }

        public String getNoticequalitys() {
            return noticequalitys;
        }

        public void setNoticequalitys(String noticequalitys) {
            this.noticequalitys = noticequalitys;
        }

        public int getNotiecuptime() {
            return notiecuptime;
        }

        public void setNotiecuptime(int notiecuptime) {
            this.notiecuptime = notiecuptime;
        }

        public int getPowersearchlevel() {
            return powersearchlevel;
        }

        public void setPowersearchlevel(int powersearchlevel) {
            this.powersearchlevel = powersearchlevel;
        }

        public int getPowersearchpercent() {
            return powersearchpercent;
        }

        public void setPowersearchpercent(int powersearchpercent) {
            this.powersearchpercent = powersearchpercent;
        }

        public int getHouruptime() {
            return houruptime;
        }

        public void setHouruptime(int houruptime) {
            this.houruptime = houruptime;
        }

        public String getGiftpack() {
            return giftpack;
        }

        public void setGiftpack(String giftpack) {
            this.giftpack = giftpack;
        }

        public String getUserboss() {
            return userboss;
        }

        public void setUserboss(String userboss) {
            this.userboss = userboss;
        }

        public int getZodiac_score() {
            return zodiac_score;
        }

        public void setZodiac_score(int zodiac_score) {
            this.zodiac_score = zodiac_score;
        }
    }


//    public void initValue(UserVO userVO) {
//
//        long hasDiamond = userVO.getHasdiamond();
//        int vip = 0;
//        for (int i = 0; i < Const.user_vip_pay_min.length; i++) {
//            hasDiamond -= Const.user_vip_pay_min[i];
//            if (hasDiamond >= 0) {
//                vip++;
//            } else {
//                hasDiamond += Const.user_vip_pay_min[vip];
//                break;
//            }
//        }
//
//        if (vip >= 10) {
//            mVipNow = 0;
//            mVipTotal = 100000;
//        } else {
//            mVipNow = (int) hasDiamond;
//            mVipTotal = Const.user_vip_pay_min[vip];
//        }
//
//        if (vip > mVip) {
//            if (vip == 6) {
//                Event.addGiftEvent(mId + "", "VIP", 10, 0, 0, 0, -1, 0, 1008, 1, 0);
//            }
//            mVip = vip;
//            DBCommon.executeUpdate("update starwork_user set uservip=" + mVip + " where id=" + mId);
//        }
//
//        setMaxCardNum();
//
//        mCardIdArray = map.get("cardidarray").toString();
//        mCardIndexArray = map.get("cardindexarray").toString();
//
//        mCardIdBattle = map.get("cardidbattle").toString();
//        mCardIdNoBattle = map.get("cardidnobattle").toString();
//        mWinCount = Integer.parseInt(map.get("wincount").toString());
//        mLoseCount = Integer.parseInt(map.get("losecount").toString());
//
//        mArenaInfo = map.get("arenainfo").toString();
//
//        mMissionId = Integer.parseInt(map.get("missionid").toString());
//        mMissionStatus = Integer.parseInt(map.get("missionstatus").toString());
//
//        mFansId = Integer.parseInt(map.get("fansid").toString());
//        mFansDonate = Long.parseLong(map.get("fansdonate").toString());
//        mFansRemain = Long.parseLong(map.get("fansremain").toString());
//        mFansToday = Long.parseLong(map.get("fanstoday").toString());
//        mFansBuffer = Integer.parseInt(map.get("fansbuffer").toString());
//        mFansBufferTime = Long.parseLong(map.get("fansbuffertime").toString());
//        mHomeNotice = Integer.parseInt(map.get("homenotice").toString());
//
//        mMac = map.get("mac").toString();
//        mIdfa = map.get("idfa").toString();
//
//        mLottery = Integer.parseInt(map.get("lottery").toString());
//        artdigest = Integer.parseInt(map.get("artdigest").toString());
//        partdigest = Integer.parseInt(map.get("partdigest").toString());
//
//        mDailyValue = map.get("dailyvalue").toString();
//        mDailyStatus = map.get("dailystatus").toString();
//
//        freehireovertime = Integer.parseInt(map.get("refreshmoneystore").toString());
//        hirenum = Integer.parseInt(map.get("refreshdiamondstore").toString());
//
//        hascode = Long.parseLong(map.get("hascode").toString());
//
//        mPowerSearchLevel = Integer.parseInt(map.get("powersearchlevel").toString());
//        mPowerSearchPercent = Integer.parseInt(map.get("powersearchpercent").toString());
//
//        vipDrawStatus = map.get("vipdrawstatus").toString();
//        dateNum = Integer.parseInt(map.get("datenum").toString());
//
//        cardTotalPower = Long.parseLong(map.get("cardtotalpower").toString());
//        cardTopPowerIcons = map.get("cardtoppowericons").toString();
//        cardTopPowerStarLevels = map.get("cardtoppowerstarlevels").toString();
//        cardTopPowerProLevels = map.get("cardtoppowerprolevels").toString();
//        cardTopPowerProTypes = map.get("cardtoppowerprotypes").toString();
//        cardTopPowerPops = map.get("cardtoppowerpops").toString();
//        cardTopPowerSexs = map.get("cardtoppowersexs").toString();
//
//        if (map.get("zodiac_score") == null) {
//            zodiac_score = 0;
//        } else {
//            zodiac_score = NumberUtils.toInt(map.get("zodiac_score").toString());
//        }
//
//        userNotice = new UserNotice(map);
//
//        Object giftPackObj = map.get("giftpack");
//        if (giftPackObj != null) {
//            giftPack = new GiftPack(mId, giftPackObj.toString());
//        } else {
//            giftPack = new GiftPack(mId, "");
//        }
//
//        if (map.get("userboss") != null) {
//            boss = new UserBoss(this, map.get("userboss").toString());
//        } else {
//            boss = new UserBoss(this);
//        }
//
//        OnlineUserCache.set(JedisPrefix.USER_ + mId, this);
//    }

}
